﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="../internal.js"></script>
    <link rel="stylesheet" type="text/css" href="insertvideo.css" />
    <style>
        table td, table th { height: 41px; line-height: 41px; }
        table th { width: 70px; text-align: right; padding-right: 20px; font-weight: normal; }
        table input { height: 25px; line-height: 25px; padding: 0 5px; border: solid 1px #ddd; }
    </style>
</head>
<body>
    <div class="wrapper">
        <div style="padding:15px 0; font-size:13px; text-align:center;">
            目前微信只支持腾讯视频，请把视频上传到腾讯视频然后复制网站到URL
        </div>
        <table>
            <tr>
                <th>URL</th>
                <td>
                    <input id="url" style="width:300px;" />
                </td>
            </tr>
            <tr>
                <th>宽度</th>
                <td>
                    <input id="width" style="width:50px;" value="300" />
                </td>
            </tr>
            <tr>
                <th>高度</th>
                <td>
                    <input id="height" style="width:50px;" value="200" />
                </td>
            </tr>
        </table>
    </div>
    <script type="text/javascript">

        function validate()
        {
            var url = document.getElementById("url").value.trim();
            if (url.length == 0)
            {
                alert("请输入URL");
                return false;
            }
            var urlReg = new RegExp("((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?");
            if (!urlReg.test(url))
            {
                alert("不是有效的URL");
                return false;
            }
            if (url.indexOf("http://v.qq.com/") < 0 && url.indexOf("https://v.qq.com/") < 0)
            {
                alert("不是有效的腾讯视频URL");
                return false;
            }

            var width = document.getElementById("width").value.trim();
            if (width.length == 0)
            {
                alert("请输入宽度");
                return false;
            }
            var widthReg = new RegExp("^[0-9]*$");
            if (!widthReg.test(width))
            {
                alert("宽度必须是数字");
                return false;
            }

            var height = document.getElementById("height").value.trim();
            if (height.length == 0)
            {
                alert("请输入高度");
                return false;
            }
            var heightReg = new RegExp("^[0-9]*$");
            if (!heightReg.test(height))
            {
                alert("高度必须是数字");
                return false;
            }

            return true;
        }

        function query(urlstring, name)
        {
            name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regexS = "[\\?&]" + name + "=([^&#]*)";
            var regex = new RegExp(regexS);
            var results = regex.exec(urlstring);
            if (results == null)
                return "";
            else
                return decodeURIComponent(results[1].replace(/\+/g, " "));
        };

        function getVid(url)
        {
            //var result = url.match(/[?\&]vid=([^\&]+)/i);
            //if (!result)
            //{
            //    result = url.match(/u\/([^\.]+)/i);
            //}
            //if (!result)
            //{
            //    return "";
            //}
            //return result[1];
            if (url.indexOf('vid') > 0) {
               return query(url, "vid");
            }

            return url.substring(url.indexOf('page/') + 5, url.indexOf('.html'));
        }

        window.onload = function ()
        {
            dialog.onok = function ()
            {
                if (!validate()) return false;

                var str = "";
                var url = document.getElementById("url").value.trim();
                var width = document.getElementById("width").value.trim();
                var height = document.getElementById("height").value.trim();
                var vid = getVid(url);

                if (url.indexOf("https") >= 0) {
                    str += "<center><iframe width=\"" + width + "\" height=\"" + height + "\" src=\"https://v.qq.com/iframe/player.html?vid=" + vid + "&width=" + width + "&height=" + height + "&auto=0\" frameborder=\"0\"></iframe></center>";
                } else {
                    str += "<center><iframe width=\"" + width + "\" height=\"" + height + "\" src=\"http://v.qq.com/iframe/player.html?vid=" + vid + "&width=" + width + "&height=" + height + "&auto=0\" frameborder=\"0\"></iframe></center>";
                }
                
                editor.execCommand("insertHTML", str);
            };
            dialog.oncancel = function ()
            {
            }
        };

    </script>

</body>
</html>
